package com.yikao.mini.demo.user;

import com.yikao.mini.demo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Controller
public class UserControl {
    //必须要加@Mapper，不然Spring不会扫到这些类

    @Autowired
    private RxzYikaoMiniUserMapper rxzYikaoMiniUserMapper;

    //返回所有教官信息
    @RequestMapping(path = "/demo/users")
    @ResponseBody
    public Result<List<RxzYikaoMiniUser>> signup() {
//    RxzYikaoMiniUser rUser = new RxzYikaoMiniUser();
        Result<List<RxzYikaoMiniUser>> result = new Result<>();
        List<RxzYikaoMiniUser> users = rxzYikaoMiniUserMapper.selectAll();
        result.setData(users);
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(path = "/yikao/user/{id}")
    @ResponseBody
    public Result<RxzYikaoMiniUser> get(@PathVariable("id") String id) {
        Result<RxzYikaoMiniUser> result = new Result<>();
        RxzYikaoMiniUser rUsers = rxzYikaoMiniUserMapper.selectByPrimaryKey(id);

        if (rUsers != null) {
            result.setData(rUsers);
            result.setSuccess(true);
            return result;
        }
        result.setMessage("no user with id:" + id);
        return result;
    }

    //    向数据库里注册教官信息
    @RequestMapping(path = "/yikao/signup")
    @ResponseBody
    public Result<User> signup(@RequestBody User user) {
//        往前端返回数据
        Result result = new Result();
        result.setData(user);
        //        往数据库里存数据：
        RxzYikaoMiniUser rUser = new RxzYikaoMiniUser();
        rUser.setId(UUID.randomUUID().toString());
        rUser.setName(user.name);
        rUser.setMobile(user.mobile);
        rxzYikaoMiniUserMapper.insert(rUser);
        //        值已经传到后台
        System.out.println(user.mobile);
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(path = "/yikao/update")
    @ResponseBody
    public Result<User> update(@RequestBody User user) {
//        System.out.println(user.id);
        //        往前端返回数据
        Result result = new Result();
        result.setData(user);
        RxzYikaoMiniUser rUser = new RxzYikaoMiniUser();
        rUser.setId(user.id);
        rUser.setName(user.name);
        rUser.setMobile(user.mobile);
//        mapper跟数据库操作取值有关，result是前端操作返回值有关
        rxzYikaoMiniUserMapper.updateByPrimaryKey(rUser);
        //        值已经传到后台
//        System.out.println(user.mobile);
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(path = "/yikao/delete/{id}")
    @ResponseBody
    public Result<User> update(@PathVariable("id")  String id) {
        RxzYikaoMiniUser rUser = new RxzYikaoMiniUser();
//        User user = new User();
//        System.out.println(user.id);
        Result result = new Result();
        rxzYikaoMiniUserMapper.deleteByPrimaryKey(id);
        return result;
    }

    //模糊搜索：
    @RequestMapping(path = "/yikao/like_search")
    @ResponseBody
    public Result<RxzYikaoMiniUser> fuzzyQuery(@RequestParam("keyword") String keyword) {
                System.out.println(keyword);
        Result result = new Result();
        List<RxzYikaoMiniUser> rxzYikaoMiniUser = rxzYikaoMiniUserMapper.fuzzySelectU(keyword);
        if (!CollectionUtils.isEmpty(rxzYikaoMiniUser)) {
            result.setSuccess(true);
            result.setData(rxzYikaoMiniUser);
            return result;
        }
        result.setSuccess(false);
        return result;
    }


}